package physicsModeler;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import physicsModeler.environment.DrawEnvironment;
import physicsModeler.objects.DrawObject;

public class DrawToolBar  implements MouseListener {
	
	protected int screenWidth;
	protected int screenHeight;
	ArrayList<DrawObject> UIActiveObjects;
	ArrayList<DrawEnvironment> UIStaticObjects;
	Screen myScreen;
	String modelSelected;
	Graphics2D leftMenu,rightMenu,header, uiBox;
	Font f2 = new Font("Arial", Font.BOLD, 16);
	
	public DrawToolBar(Screen screen)
	{
		UIActiveObjects = new ArrayList<DrawObject>();
		UIStaticObjects = new ArrayList<DrawEnvironment>();
		myScreen = screen;
		screenWidth = myScreen.getWidth();
		screenHeight = myScreen.getHeight();
		modelSelected = "Select an object";
	}
	
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public void Draw(Graphics2D graphics, double avgFPS)
	{
		
		drawLeftMenu(graphics);
		drawRightMenu(graphics);
		drawHeader(graphics,modelSelected,avgFPS);
		
		for(DrawObject object: UIActiveObjects)
		{
			String name = object.getName();
			int positionX = object.getPositionX();
			int positionY = object.getPositionY();
			createBoundingBox(graphics,name,positionX,positionY,50,50);
			//object.draw(graphics);
			
		}
		
		for(DrawEnvironment object: UIStaticObjects)
		{
			//object.draw(graphics);
			//if(object.getName()!= "")
			String name = object.getName();
			int positionX = object.getPositionX();
			int positionY = object.getPositionY();
			createBoundingBox(graphics,name,positionX,positionY,50,50);
		}
		
		
	}
	
	public void drawLeftMenu(Graphics2D graphics)
	{
		
		leftMenu = graphics;
		leftMenu.setFont(f2);
		leftMenu.setColor(Color.red);
		leftMenu.drawRect(0, 0, 200, 900);
		leftMenu.drawString("Active Objects", 25, 50);
		
	}
	
	
	public void drawRightMenu(Graphics2D graphics)
	{
		rightMenu = graphics;
		rightMenu.setFont(f2);
		rightMenu.setColor(Color.red);
		rightMenu.drawRect(screenWidth-200, 0, 199, screenHeight);
		rightMenu.drawString("Static Objects", screenWidth-150, 50);
		
	}
	
	public void drawHeader(Graphics2D graphics,String model, double avgFPS)
	{
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		avgFPS = Double.valueOf(twoDForm.format(avgFPS));
		header = graphics;
		header.setFont(new Font("Arial", Font.BOLD, 18));
		header.setColor(Color.red);
		header.drawRect(200, 0, screenWidth-400, 100);
		header.drawString("Physics Modeler", screenWidth/2-100, 50);
		header.setFont(f2);
		header.drawString("Model Selected: " + model,250,50);
		
		header.drawString("FPS: " + avgFPS, screenWidth-350, 50);
		
	}
	
	public void createBoundingBox(Graphics2D graphics,String name, int posX,int posY,int widthX, int widthY)
	{
		uiBox = graphics;
		uiBox.setFont(new Font("Arial", Font.PLAIN, 14));
		uiBox.setColor(Color.red);
		uiBox.fillRect(posX,posY,widthX,widthY);
		uiBox.setColor(Color.WHITE);
		uiBox.drawString(name,posX,posY-10);
		
	}
	
	
	public void loadUIObjects(ObjectFactory objectFactory,EnvironmentFactory environmentFactory)
	{
		int positionX,positionY;
		positionX = 50;
		positionY = 100;
		for(int i = 0; i < objectFactory.objNames.size(); i++)
		{
			String name = (String)objectFactory.objNames.elementAt(i);
			try 
			{
				DrawObject tempObject = (DrawObject)objectFactory.createDrawObject(name);
				tempObject.setSizeX(25);
				tempObject.setSizeY(25);
				tempObject.setPositionX(positionX);
				tempObject.setPositionY(positionY);
				positionY += 100;
				UIActiveObjects.add(tempObject);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		positionX = myScreen.getWidth() - 150;
		positionY = 100;
		
		for(int i = 0; i < environmentFactory.envNames.size(); i++)
		{
			String name = (String)environmentFactory.envNames.elementAt(i);
			try 
			{
				DrawEnvironment tempEnvObject = (DrawEnvironment)environmentFactory.createDrawEnvironment(name);
				tempEnvObject.setSizeX(25);
				tempEnvObject.setSizeY(25);
				tempEnvObject.setPositionX(positionX);
				tempEnvObject.setPositionY(positionY);
				positionY += 100;
				UIStaticObjects.add(tempEnvObject);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	public Map<String,String> checkItemSelected(int xPosition, int yPosition)
	{
		Map<String,String> itemSelected = new HashMap<String,String>();
		if(xPosition < myScreen.getWidth()/2)
		{
			// Check Active Objects
			for(DrawObject object: UIActiveObjects)
			{
				if(xPosition - object.getPositionX() < 50)
				{
					if(yPosition - object.getPositionY() < 50)
					{
						modelSelected = object.getName();
						itemSelected.put(object.getCategory(), object.getName());
						return itemSelected;
					}
					
				}
				
			}	
		}
		else
		{
			// Check Static Objects
			// Check Active Objects
			for(DrawEnvironment object: UIStaticObjects)
			{
				if(object.getPositionX() - xPosition < 50)
				{
					if(object.getPositionY() - yPosition < 50)
					{
						itemSelected.put(object.getCategory(), object.getName());
						return itemSelected;
					}
					
				}
				
			}	
			
		}
		
		return null;
	}
	
	

	
	
	
	
	
	
	
	
	
	
	/*
	private String objItem;
	private JComboBox objCombo,envCombo; 
	private JButton b; 
	
	
	
	public DrawToolBar()
	{
		width = 200;
		height = 400;
		super.setSize(width, height);
	}
	
	public DrawToolBar(String title)
	{
		super(title);
		width = 200;
		height = 400;
		super.setSize(width, height);
		handler = new Handler();
        
		
	}
	
	protected void frameInit(){
		
		
		ObjectFactory objectFactory = new ObjectFactory();
		EnvironmentFactory environmentFactory = new EnvironmentFactory();
		objCombo = new JComboBox();
		objCombo.setActionCommand("objCombo");
		objCombo.addActionListener(this);
		
		envCombo = new JComboBox();
		envCombo.addActionListener(this);
		envCombo.setActionCommand("envCombo");
		
		b = new JButton("Run Simulation");
		b.addActionListener(this);
		b.setActionCommand("RunSimulation");
		Vector objTypes = objectFactory.getShapeNames();
		Vector envTypes = environmentFactory.getEnvNames();
		super.frameInit();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		for (int i = 0; i < objTypes.size(); i++){
			objCombo.addItem(objTypes.get(i));
		}
		
		for (int i=0;i< envTypes.size();i++)
		{
			envCombo.addItem(envTypes.get(i));
			
		}
	    
		setLayout(new FlowLayout());
	    //cp.add(t);
	    add(objCombo);
	    add(envCombo);
	    add(b);
	    
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if("RunSimulation".equals(e.getActionCommand()))
		{
			handler.Run();
			
		}
		
		if("objCombo".equals(e.getActionCommand()))
		{
			objItem = objCombo.getSelectedItem().toString();
		}
	
		
	}

	public void setObjItem(String objItem) {
		this.objItem = objItem;
	}

	public String getObjItem() {
		return objItem;
	}
	
	*/
	
	

}
